package com.googlecode.jmicroerp.wicket;

import com.googlecode.jmicroerp.wicket.administrare.UtilizatoriViewPage;
import com.googlecode.jmicroerp.wicket.administrare.VizualizareMagazinePage;
import com.googlecode.jmicroerp.wicket.administrare.VizualizareProdusePage;
import com.googlecode.jmicroerp.wicket.vanzare.PosPage;
import com.googlecode.jmicroerp.wicket.butoaie.ButoaiePage;
import com.googlecode.jmicroerp.wicket.reports.ReportsPage;
//import com.googlecode.jmicroerp.wicket.stocuri.VizualizareStocPage;

import com.googlecode.jmicroerp.model.Utilizator;
import com.googlecode.jmicroerp.model.Rol;
import com.googlecode.jmicroerp.wicket.administrare.VizualizareCategoriiPage;
import com.googlecode.jmicroerp.wicket.administrare.VizualizareRecipientePage;
import com.googlecode.jmicroerp.wicket.administrare.VizualizareStocuriPage;
import com.googlecode.jmicroerp.wicket.administrare.VizualizareZZZPage;
import com.googlecode.jmicroerp.wicket.administrare.VizualizareApometrePage;
import com.googlecode.jmicroerp.wicket.administrare.VizualizareTipRecipientePage;
import com.googlecode.jmicroerp.service.GenericService;
import com.googlecode.jmicroerp.repository.GenericRestriction;
import com.googlecode.jmicroerp.wicket.index.IndexPage;
import com.googlecode.jmicroerp.wicket.login.LoginPage;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.springframework.security.core.GrantedAuthority;
import org.wicketstuff.security.components.SecureWebPage;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 * @author alex
 *
 */
public class DefaultPageTemplate extends SecureWebPage {

    @SpringBean(name = "genericService")
    private GenericService<Utilizator> genericService;
    /**
     *
     */
    private static final long serialVersionUID = 8283257731507188591L;

    private Link createNavigationLink(String id, final Class<? extends WebPage> pageClass, String... roles) {
        Link link = new Link(id) {
            @Override
            public void onClick() {
                setResponsePage(pageClass);
            }
        };
        boolean visible = false;
        for (GrantedAuthority authority: getUtilizator().getAuthorities()) {
            for (String role : roles) {
                if (authority.getAuthority().equalsIgnoreCase(role)) {
                    visible = true;
                    break;
                }
            }
            if (visible) {
                break;
            }
        }
        
        link.setVisible(visible);
        return link;
    }

    protected Utilizator getUtilizator() {
        return ((SecuredWebSession) getSession()).getUserDetail().getUtilizator();
    }
    
    protected String getUsername() {
        return ((SecuredWebSession) getSession()).getUserDetail().getUsername();
    }
    
    public DefaultPageTemplate() {
        /* Link homeLink = createNavigationLink("homeLink", IndexPage.class, "ROLE_STORE_MANAGER", "ROLE_MANAGER", "ROLE_USER"); */
        
        Link homeLink = new Link("homeLink") {
            @Override
            public void onClick() {
                String username = ((SecuredWebSession) getSession()).getUserDetail().getUsername();
                GenericRestriction genericRestriction = new GenericRestriction("numeUtilizator", username);
                    Utilizator utilizatorDb = genericService.getSingleResult(Utilizator.class, genericRestriction);
                    for (Rol rol : utilizatorDb.getRoluri()) {
                        if (!"ROLE_USER".equals(rol.getDenumire())) {
                            setResponsePage(IndexPage.class);
                            return;
                        }
                    }
                    setResponsePage(PosPage.class);
            }
        };
        
        Label lblStorename = new Label("storename", "Magazin: " + ((SecuredWebSession) getSession()).getMagazin().getDenumire());
        Label lblNumeUtilizator = new Label("username", getUsername());
        Link logoutLink = new Link("logoutLink") {
            @Override
            public void onClick() {
                getSession().invalidateNow();
                setResponsePage(LoginPage.class);
            }
        };
        add(homeLink, lblStorename, logoutLink, lblNumeUtilizator);

        /* Links for user */
        Link linkIndex = createNavigationLink("dashboard", IndexPage.class, "ROLE_STORE_MANAGER", "ROLE_MANAGER");
        Link linkPosm = createNavigationLink("vanzare_menu", PosPage.class, "ROLE_STORE_MANAGER", "ROLE_MANAGER", "ROLE_USER");
        Link linkButoaie = createNavigationLink("butoaie", ButoaiePage.class, "ROLE_STORE_MANAGER", "ROLE_MANAGER", "ROLE_USER");
        Link linkReports = createNavigationLink("reports", ReportsPage.class, "ROLE_STORE_MANAGER", "ROLE_MANAGER", "ROLE_USER");
        add(linkIndex, linkPosm, linkButoaie, linkReports);
        
        /* links for admin */
        /* Link linkProgram = createNavigationLink("program", VizualizareProgramLucruPage.class); */
        Link linkUtilizatori = createNavigationLink("utilizatori", UtilizatoriViewPage.class, "ROLE_STORE_MANAGER", "ROLE_MANAGER");
        Link linkMagazine = createNavigationLink("viewMagazine", VizualizareMagazinePage.class, "ROLE_STORE_MANAGER", "ROLE_MANAGER");
        Link linkProduse = createNavigationLink("viewProduse", VizualizareProdusePage.class, "ROLE_STORE_MANAGER", "ROLE_MANAGER");
        Link linkCategorii = createNavigationLink("viewCategorii", VizualizareCategoriiPage.class, "ROLE_STORE_MANAGER", "ROLE_MANAGER");
        Link linkTipRecipient = createNavigationLink("viewTipRecipient", VizualizareTipRecipientePage.class, "ROLE_STORE_MANAGER", "ROLE_MANAGER");
        Link linkRecipient = createNavigationLink("viewRecipient", VizualizareRecipientePage.class, "ROLE_STORE_MANAGER", "ROLE_MANAGER");
        Link linkStoc = createNavigationLink("viewStoc", VizualizareStocuriPage.class, "ROLE_STORE_MANAGER", "ROLE_MANAGER");
        Link linkApometre = createNavigationLink("viewApometre", VizualizareApometrePage.class, "ROLE_STORE_MANAGER", "ROLE_MANAGER");
        Link linkZZZ = createNavigationLink("viewZZZ", VizualizareZZZPage.class, "ROLE_STORE_MANAGER", "ROLE_MANAGER");

        /* add(linkProgram, linkUtilizatori, linkMagazine, linkTipRecipient, linkRecipient, linkCategorii, linkProduse, linkStoc, linkPosm, linkReports); */
        add(linkUtilizatori, linkMagazine, linkTipRecipient, linkRecipient, linkCategorii, linkProduse, linkStoc, linkApometre, linkZZZ);

    }

    public Long getUserId() {
        return getUser().getId();
    }

    public Utilizator getUser() {
        return ((SecuredWebSession) this.getSession()).getUserDetail().getUtilizator();
    }
}
